package com.share.auth.service;

import com.share.common.core.domain.R;
import com.share.common.core.exception.ServiceException;
import com.share.common.core.utils.StringUtils;
import com.share.system.api.model.LoginUser;
import com.share.user.api.RemoteUserService;
import com.share.user.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.rowset.serial.SerialException;

@Component
public class H5LoginService {
    @Autowired
    private RemoteUserService remoteUserService;

    public LoginUser login(String code) {
        //1.判断code是否为空
        if(StringUtils.isEmpty(code)){
            throw new ServiceException("数据为空");
        }
        //2.根据code远程调用完成登录
        R<UserInfo> userInfoR = remoteUserService.wxLogin(code);
        UserInfo userInfo = userInfoR.getData();
        String status = userInfo.getStatus();
        if ("2".equals(status)){
            throw new ServiceException("用户已被禁用");
        }
        //判断userInfo是否为空
        if(userInfo == null){
            throw new ServiceException("远程调用失败");
        }

        //封装数据到userInfo中
        LoginUser loginUser = new LoginUser();
        loginUser.setUserid(userInfo.getId());
        loginUser.setUsername(userInfo.getWxOpenId());
        loginUser.setStatus(userInfo.getStatus()+"");

        //返回数据
        return loginUser;
    }
}
